// pages/addsite/addsite.js
var http = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iii:'',
    token:'',
    shippingAdd:[ ],
    pitchOn:'',
    bgc:'',
    addStatus:false
  },
  delAdd:function(e){ //该函数只负责删除
    var that = this
    if(e.currentTarget.dataset.i >= 0){
      var addId = e.currentTarget.dataset.i
      console.log(addId);
      http.myRequest("/api/userAddressDelete",{token:that.data.token,id:addId},del)
    }else{
      console.log("当前地址列表还未生成，无法删除"); 
    }
    function del(){
      console.log("已经完成删除，重新渲染");
      that.addListCha()
    }
  },
  addFn:function(_add){//地址回调公用，打印结果
    // console.log(_add);
  },
  addTheAdd:function(){
    wx.navigateTo({
      url: '../addTheadd/addTheadd',
    })
  },
  tokenFn:function(){//页面加载获取token
    // console.log("默认赋值token");
    var that = this
    wx.getStorage({
      key: 'token',
      success(res){
        that.setData({
          token:res.data
        })//拿到token之后再次传入回调,请求地址列表,作用域问题只能使用that
        that.addList()
      }
    })
  },

  addList:function(aa){//接收地址列表。只负责接收到地址
    var that = this
    if( that.data.token !=  ''){
      http.myRequest("/api/userAddressList",{token:that.data.token},listf)
    }else{
      // console.log("请先授权！"); 
    }
    function listf(_list){
      var list = _list.data
      if(list.length > 0 ){
        that.setData({
          addStatus:true
        })
      }
      // console.log(list);
      list.forEach((v,index)=>{
        if(v.address_id==aa){
          that.setData({
            iii:index
          })
        }
      })  
      that.setData({
      shippingAdd:list
      })
    }  
  },

  // addStyle:function(){
  //     var that = this
  //     http.myRequest("/api/userAddressList",{token:that.data.token},listf)
  //     function listf(_list){
  //       var list = _list.data
  //       console.log(_list);  
  //     }  
  // },

  addListCha(){
    var that = this
    console.log(that.data.token);
    
    if( that.data.token !=  ''){
        http.myRequest("/api/userAddressList",{token:that.data.token},listf)
    }else{
      console.log("请先授权！"); 
    }
    function listf(_list){
      var list = _list.data
      console.log(_list);  
      that.setData({
       shippingAdd:list
      })
    }  
  },
  pitchOn:function(e){
    var that = this
    var addId = e.currentTarget.dataset.index;
    that.setAddOn(e);
    that.addList(addId);
    that.setData({
      pitchOn:addId
    })
  },
  setAddOn:function(e){//设置默认地址
     var that = this
     var regPos = /^\d+(\.\d+)?$/; 
     if(that.data.pitchOn == ''){
      wx.showToast({
        title: '请选中默认地址！',
        icon:"none",
        mask:true
      })
     }else{
      http.myRequest("/api/userAddressDfault",{
        token:that.data.token,
        id:that.data.pitchOn
      },setAdd)
     }
      function setAdd(res){
        wx.showToast({
          // title: '默认地址设置成功！',
          mask:true
        })
        that.onReady()
        setTimeout(nav,1000  )
      }
      function nav(){
        wx.navigateBack({  })
      }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.tokenFn()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.addList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})